#pragma once

template<class K, class V>
struct BSTreeNode
{
	BSTreeNode(const K& key = K(), const V& val = V())
		:_key(key)
		,_val(val)
		,_left(nullptr)
		,_right(nullptr)
	{}
	K _key;
	V _val;
	BSTreeNode<K,V>* _left;
	BSTreeNode<K,V>* _right;
};

template<class K, class V>
class BSTree
{
	typedef BSTreeNode<K,V> Node;
public:
	BSTree()
		:_root(nullptr)
	{}

	BSTree(const BSTree<K,V>& t)
	{
		_root = Copy(t._root);
	}

	~BSTree()
	{
		_Destory(_root);
	}

	BSTree<K,V>& operator=(BSTree<K,V> t)
	{
		std::swap(_root, t._root);
		return *this;
	}

	bool Insert(const K& key,const V& val)
	{
		if (_root == nullptr)
		{
			_root = new Node(key,val);
			return true;
		}
		else
		{
			Node* parent = _root;
			Node* cur = _root;
			while (cur != nullptr)
			{
				if (cur->_key == key)
				{
					return false;
				}
				else if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else
				{
					parent = cur;
					cur = cur->_right;
				}
			}
			cur = new Node(key,val);
			if (parent->_key > key)
			{
				parent->_left = cur;
			}
			else if (parent->_key < key)
			{
				parent->_right = cur;
			}
			return true;
		}
	}
	void InOrder()
	{
		_InOrder(_root);
	}

	Node* Find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (cur->_key == key)
			{
				return cur;
			}
			else if (cur->_key > key)
			{
				cur = cur->_left;
			}
			else if (cur->_key < key)
			{
				cur = cur->_right;
			}
		}
		return nullptr;
	}

	bool Erase1(const K& key)
	{
		Node* cur = _root;
		Node* parent = nullptr;
		while (cur)
		{
			if (cur->_key > key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else
			{
				if (parent == nullptr)
				{
					if (cur->_left == nullptr)
					{
						_root = cur->_right;
						delete cur;
						return true;
					}
					else if (cur->_right == nullptr)
					{
						_root = cur->_left;
						delete cur;
						return true;
					}
					else
					{
						Node* leftMaxParent = cur;
						Node* leftMax = cur->_left;
						if (leftMax->_right == nullptr)
						{
							leftMax->_right = cur->_right;
							delete cur;
							_root = leftMax;
							return true;
						}
						while (leftMax->_right)
						{
							leftMaxParent = leftMax;
							leftMax = leftMax->_right;
						}
						std::swap(leftMax->_key, cur->_key);
						std::swap(leftMax->_val, cur->_val);

						leftMaxParent->_right = leftMax->_left;
						delete leftMax;
						leftMax = nullptr;
						return true;
					}
				}
				if (parent->_left == cur)
				{
					if (cur->_left == nullptr)
					{
						parent->_left = cur->_right;
						delete cur;
						return true;
					}
					else if (cur->_right == nullptr)
					{
						parent->_left = cur->_left;
						delete cur;
						return true;
					}
					else 
					{
						Node* leftMaxParent = cur;
						Node* leftMax = cur->_left;
						if (leftMax->_right == nullptr)
						{
							leftMax->_right = cur->_right;
							delete cur;
							parent->_left = leftMax;
							return true;
						}
						while (leftMax->_right)
						{
							leftMaxParent = leftMax;
							leftMax = leftMax->_right;
						}
						std::swap(leftMax->_key, cur->_key);
						std::swap(leftMax->_val, cur->_val);

						leftMaxParent->_right = leftMax->_left;
						delete leftMax;
						leftMax = nullptr;
						return true;
					}
				}
				else
				{
					if (cur->_left == nullptr)
					{
						parent->_right = cur->_right;
						delete cur;
						return true;
					}
					else if (cur->_right == nullptr)
					{
						parent->_right = cur->_left;
						delete cur;
						return true;
					}
					else
					{
						Node* leftMaxParent = cur;
						Node* leftMax = cur->_left;
						if (leftMax->_right == nullptr)
						{
							leftMax->_right = cur->_right;
							delete cur;
							parent->_right = leftMax;
							return true;
						}
						while (leftMax->_right)
						{
							leftMaxParent = leftMax;
							leftMax = leftMax->_right;
						}
						std::swap(leftMax->_key, cur->_key);
						std::swap(leftMax->_val, cur->_val);

						leftMaxParent->_right = leftMax->_left;
						delete leftMax;
						leftMax = nullptr;
						return true;
					}
				}
			}
		}
		return false;
	}

	bool Erase2(const K& key)
	{
		Node* cur = _root;
		Node* parent = nullptr;
		while (cur)
		{
			if (cur->_key > key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else
			{
				if (cur->_left == nullptr)
				{
					if (parent == nullptr)
					{
						_root = cur->_right;
					}
					else if (parent->_left == cur)
					{
						parent->_left = cur->_right;
					}
					else if (parent->_right == cur)
					{
						parent->_right = cur->_right;
					}
				}
				else if (cur->_right == nullptr)
				{
					if (parent == nullptr)
					{
						_root = cur->_left;
					}
					else if (parent->_left == cur)
					{
						parent->_left = cur->_left;
					}
					else if (parent->_right = cur)
					{
						parent->_right = cur->_left;
					}
				}
				else
				{
					Node* leftMax = cur->_left;
					Node* leftMaxParent = cur;
					while (leftMax->_right)
					{
						leftMaxParent = leftMax;
						leftMax = leftMax->_right;
					}
					std::swap(cur->_key, leftMax->_key);
					std::swap(cur->_val, leftMax->_val);

					if (leftMaxParent->_left == leftMax)
					{
						leftMaxParent->_left = leftMax->_left;
					}
					else
					{
						leftMaxParent->_right = leftMax->_left;
					}

					cur = leftMax;
				}
				delete cur;
				return true;
			}
		}
	}

	Node* FindR(const K& key)
	{
		return _FindR(_root, key);
	}

	bool InsertR(const K& key,const V& val)
	{
		return _InsertR(_root, key, val);
	}

	bool EraseR(const K& key)
	{
		return _EraseR(_root, key);
	}
private:
	Node* Copy(Node* root)
	{
		if (root == nullptr)
		{
			return nullptr;
		}
		Node* Copyroot = new Node(root->_key, root->val);
		Copyroot->_left = Copy(root->_left);
		Copyroot->_right = Copy(root->_right);

		return Copyroot;
	}

	void _Destory(Node*& root)
	{
		if (root == nullptr)
		{
			return;
		}
		_Destory(root->_left);
		_Destory(root->_right);
		delete root;
		root == nullptr;
	}
	bool _EraseR(Node*& root, const K& key)
	{
		if (root == nullptr)
		{
			return false;
		}
		if (root->_key < key)
		{
			return _EraseR(root->_right, key);
		}
		else if (root->_key > key)
		{
			return _EraseR(root->_left, key);
		}
		else
		{
			Node* del = root;
			if (root->_left == nullptr)
			{
				root = root->_right;
			}
			else if (root->_right == nullptr)
			{
				root = root->_left;
			}
			else
			{
				Node* leftMax = root->_left;
				while (leftMax->_right)
				{
					leftMax = leftMax->_right;
				}
				std::swap(leftMax->_key, root->_key);
				std::swap(leftMax->_val, root->_val);

				return _EraseR(root->_left, key);
			}
			delete del;
			return true;
		}
	}
	bool _InsertR(Node*& root, const K& key, const V& val)
	{
		if (root == nullptr)
		{
			root = new Node(key, val);
			return true;
		}
		if (root->_key < key)
		{
			return _InsertR(root->_right, key, val);
		}
		else if (root->_key > key)
		{
			return _InsertR(root->_left, key, val);
		}
		else
		{
			return false;
		}
	}
	Node* _FindR(Node* root, const K& key)
	{
		if (root == nullptr)
		{
			return nullptr;
		}
		if (root->_key == key)
		{
			return root;
		}
		else if (root->_key > key)
		{
			return _FindR(root->_left, key);
		}
		else
		{
			return  _FindR(root->_right, key);
		}
	}
	void _InOrder(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}
		_InOrder(root->_left);
		cout << root->_key << " :" << root->_val << endl;
		_InOrder(root->_right);
	}
private:
	Node* _root;
};